<?php

class KindEditor extends CInputWidget{
	private $language = 'zh_CN';
	public $width='90%';
	public $height='350px';
	public $themes='default';
    public $value;
    public $name;
	public $resizeType=0;//是否允许拖动
    public $toolbar = 'full';
    public $isfunc = false;
	public $item="['source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
        'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
        'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
        'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
        'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
        'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
        'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
        'anchor', 'link', 'unlink', '|', 'about']";
	public function makeOptions()
	{
		//list($name, $id) = $this->resolveNameID();
        $id = $this->name;
		$assets = Yii::app()->request->baseUrl.'/static/plugins/kindeditor/';
		$uploadJson = $assets . '/php/upload_json.php';
		$fileManagerJson = $assets . '/php/file_manager_json.php';
        $width = $this->width;
        $heigth = $this->height;
        if( $this->toolbar=='simple' ){
            $this->item = "['emoticons','|','justifyleft', 'justifycenter', 'justifyright','justifyfull','|','fontsize','forecolor','bold','italic','underline']";
        }
		$script = " var editor;
		            $(function() {
                    	editor = KindEditor.create('textarea[id=\"{$id}\"]', {
                        		'uploadJson' : '{$uploadJson}',
                        		'fileManagerJson' : '{$fileManagerJson}',
                        		'allowFileManager' : true,
                        		'resizeType' : {$this->resizeType},
                        		'items':{$this->item},
                                'width' : '$width',
	                            'height' : '$heigth'
                        	    });
                	    editor.sync();
                	});";
		return $script;
	}

    public function run(){
        parent::run();
        $assets = Yii::app()->request->baseUrl.'/static/plugins/kindeditor';
        $cs = Yii::app()->getClientScript();
        $cs->registerCssFile($assets.'/themes/'.$this->themes.'/'.$this->themes.'.css');
        $cs->registerCssFile($assets.'/plugins/code/prettify.css');
        $cs->registerScriptFile($assets.'/kindeditor.js',CClientScript::POS_HEAD);
        $cs->registerScriptFile($assets.'/lang/zh_CN.js',CClientScript::POS_HEAD);
        $cs->registerScriptFile($assets.'/plugins/code/prettify.js',CClientScript::POS_HEAD);
        
        //list($name, $id) = $this->resolveNameID();
        if( ! $this->isfunc ) echo CHtml::textArea($this->name,$this->value,array('style'=>'width:'.$this->width.';height:'.$this->height));
		
        
        $cs->registerScript('content',$this->makeOptions(),CClientScript::POS_END);
        //return CHtml::textArea($this->name,$this->value,array('style'=>'width:'.$this->width.';height:'.$this->height));
        //return $sss;
    }
}
?>